import { getProfile, updateUserInfo, updateUserPhoto, uppdateUsergender } from '@/api/user.js'
export default {
  namespaced: true,
  state: {
    userInfo: {}
  },
  mutations: {
    msetUserInfo (state, newUserInfo) {
      state.userInfo = newUserInfo
    },
    // 改用户名
    mUpdateUserName (state, newName) {
      state.userInfo.name = newName
    },
    // 改头像
    mUpdateUserPhoto (state, newPhoto) {
      state.userInfo.photo = newPhoto
    },
    // 改性别
    mUpdateUsergender (state, newGender) {
      state.userInfo.photo = newGender
    }
  },
  actions: {
    async getProfile (context) {
      const res = await getProfile()
      console.log('mutations,getProfile', res)
      context.commit('msetUserInfo', res.data.data)
    },
    // 改用户名
    async updateUserName (context, newName) {
      try {
        const res = await updateUserInfo({ name: newName })
        console.log('mutations,getProfile', res)
        context.commit('mUpdateUserName', newName)
      } catch (err) {
        console.log(err)
        throw new Error(err)
      }
    },
    // 改头像
    async updateUserPhoto (context, fd) {
      try {
        const res = await updateUserPhoto(fd)
        console.log('updateUserPhoto', res)
        context.commit('mUpdateUserPhoto', res.data.data.photo)
      } catch (err) {
        console.log(err)
        throw new Error(err)
      }
    },
    // 改性别
    async uppdateUsergender (context, gender) {
      try {
        const res = await uppdateUsergender({ gender: gender })
        console.log('uppdateUsergender', res)
        context.commit('mUpdateUsergender', gender)
      } catch (err) {
        console.log(err)
        throw new Error(err)
      }
    }
  }

}
